import math
# 导入math模块

# 定义等比数列求和函数
def geometric_series_sum(n):
    # 等比数列求和公式: S_n = a * (1 - r^n) / (1 - r)
    a = 1/2  # 首项
    r = 1/2  # 公比
    return a * (1 - r**n) / (1 - r)

# 打开或创建一个文本文件，准备写入
with open('geometric_series.txt', 'w', encoding='utf-8') as file:
    for n in range(1, 100):  # 计算前99项
        series = ' + '.join([f'1/{2**i}' for i in range(1, n+1)])
        sum_n = geometric_series_sum(n)
        # 将每一行的算式及其和写入文件
        file.write(f'{series} = {sum_n}\n')

print("等比数列的前99行已成功写入文件。")